<!--
 * @Author: ZhaoDong
 * @Date: 2022-07-07 18:18:42
 * @LastEditors: ZhaoDong
 * @LastEditTime: 2022-07-08 12:10:38
 * @Description: SvgIcon
-->
<template>
  <svg aria-hidden="true" class="svg-icon" :style="getStyle">
    <use :href="symbolId" :fill="color" />
  </svg>
</template>

<script lang="ts">
  import type { CSSProperties } from 'vue';
  import { defineComponent, computed } from 'vue';

  export default defineComponent({
    name: 'SvgIcon',
    props: {
      prefix: {
        type: String,
        default: 'icon'
      },
      name: {
        type: String,
        required: true
      },
      color: {
        type: String,
        default: '#333'
      },
      size: {
        type: [Number, String],
        default: 16
      }
    },
    setup(props) {
      const symbolId = computed(() => `#${props.prefix}-${props.name}`);
      const getStyle = computed((): CSSProperties => {
        const { size } = props;
        let s = `${size}`;
        s = `${s.replace('px', '')}px`;
        return {
          width: s,
          height: s
        };
      });
      return { symbolId, getStyle };
    }
  });
</script>

<style lang="scss" scoped>
  .svg-icon {
    display: inline-block;
    overflow: hidden;
    vertical-align: -0.15em;
    fill: currentcolor;
  }
</style>
